local LevelType = {
    unPass = 0, --未通关
    pass = 1,   --已通关
    elite = 2,  --小boss关
    arrive = 3, --到达
    boss = 4,   -- 大boss关
    more = 5,   -- 多队
}
local weather = {"AutoFightMap_cloudy","AutoFightMap_rain","AutoFightMap_snow","AutoFightMap_sunny"}
local firstRandom = false
---@class UIBattleBGMap :UIComponentInterface
local UIBattleBGMap = Class("UIBattleBGMap",UIComponentInterface)
function UIBattleBGMap:ctor()
    self.isCreate = false
end

function UIBattleBGMap:OnCreate()
    if self.isCreate then return end
    self.fguiObj = CreateBGUIPanel("FUI/BattleUI","UINewOfflineSence")
    if not self.fguiObj then
        return
    end
    self.isCreate = true
    self.isDestroy = false
    self.btnMap=self.fguiObj:GetChild("Btn_BigMap")
    self.btnMap.onClick:Add(function ()
        if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.BigMap, true, self.btnMap) then
            showUI(PanelNames.UIBigMap, plunderHandler.have_search);
        end
    end)
    self.ctrlStoryMode = self:GetController("storyMode")
    self.ctrlChapter = self:GetController("Chapter")
    ---@type GGraph
    self.Graph_Weather = self:GetChild("Graph_Weather")
    ---@type GGraph
    self.Graph_YanWu = self:GetChild("Graph_YanWu")
    ---@type GoWrapper
    self.goWrapper = GoWrapper.New()
    ---@type GoWrapper
    self.goWrapperSmoke = GoWrapper.New()
    ---@type FairyGUI.GLoader3D
    self.ChapterBoxEff = nil
    self.weatherTime = 0
    self.weatherIndex = 0
    self.weatherChangeTime = dataTable.getGlobalInt(235)
    ---@type GGraph[]
    self.bgEffectGraphList = {}
    ---@type {obj:GameObject,wrap:GoWrapper}[]
    self.bgEffectDataList = {}
    ---@type GComponent[]
    self.levelItemList = {}
    self.levelCfgList = {}
    self.sortIndexList = {}
    self.curMapId = -1
    self.curChapterId = -1
    self.oldChapterId = -1
    self.rewardChapter = 1
    self.hasPreChapterReward = false
    self.randomRob = nil
    self.bgEffRoot = self:GetChild("Com_MapSpine")
    self.bgEffectGraphList["effect_bingxueshanfeng"] = self:GetChild("effect_bingxueshanfeng",self.bgEffRoot)
    self.bgEffectGraphList["effect_fengyeshulin"] = self:GetChild("effect_fengyeshulin",self.bgEffRoot)
    self.bgEffectGraphList["effect_hongseshuijing"] = self:GetChild("effect_hongseshuijing",self.bgEffRoot)
    self.bgEffectGraphList["effect_huangliangdamo"] = self:GetChild("effect_huangliangdamo",self.bgEffRoot)
    self.bgEffectGraphList["effect_huoshanzhidian"] = self:GetChild("effect_huoshanzhidian",self.bgEffRoot)
    self.bgEffectGraphList["effect_senlingshenchu"] = self:GetChild("effect_senlingshenchu",self.bgEffRoot)
    self.bgEffectGraphList["effect_shanfengxiagu01"] = self:GetChild("effect_shanfengxiagu01",self.bgEffRoot)
    self.bgEffectGraphList["effect_shanfengxiagu02"] = self:GetChild("effect_shanfengxiagu02",self.bgEffRoot)
    self.bgEffectGraphList["effect_shanjianheliu"] = self:GetChild("effect_shanjianheliu",self.bgEffRoot)
    self.bgEffectGraphList["effect_zhaozezhidi"] = self:GetChild("effect_zhaozezhidi",self.bgEffRoot)

    for i = 1,60 do
        local item = self:GetChild("Com_ChapterLevel"..i)
        if item then
            item.name = tostring(i)
            table.insert(self.levelItemList,item)
        else
            break
        end
    end
    if not firstRandom then
        self.weatherIndex = math.max(math.random(1,#weather) -1,0)
        firstRandom = true
    end
    self:LoadSmoke()
    self:UpdateWeatherTime()
    self:TimeCall(self.UpdateWeatherTime,10000,-1)
    ListenEvent(Config.EventType.ChapterTaskRewardGetDone,self.ChapterTaskRewardGetDoneMsg,self)
    ListenEvent(Config.EventType.Refresh_PlunderInfo,self.Refresh_PlunderInfo,self)
    ListenEvent(Config.EventType.Refresh_OpenPlunder,self.Refresh_PlunderInfo,self)
end
function UIBattleBGMap:ShowBossReward()
    self:DoRefresh()
end
function UIBattleBGMap:Refresh(isShowAni)
    local isLastLevel = MapManager.IsEndChapterMapId(MapManager.getMapId())
    local hasChapterReward ,isChapterGet = logicMgr.MapManager.GetChapterReward(MapManager.getMapId())
    --要先领奖励
    if isLastLevel and hasChapterReward and not isChapterGet then
        self:DoRefresh()
        return
    end
    if isShowAni then
        self:ShowAnim()
    else
        self:DoRefresh(true)
    end
end
function UIBattleBGMap:ShowAnim()
    local id = MapManager.getMapId()
    local showChapterAnim = false
    local current = dataTable.getDataTableByStringId("t_stage",id)
    if current and current.f_NextStage > 0 then
        id = current.f_NextStage
        local next = dataTable.getDataTableByStringId("t_stage",id)
        if next and next.f_ChapterId ~= current.f_ChapterId then
            showChapterAnim = true
        end
    end
    self:DoRefresh()
    if showChapterAnim then
        self:FadeOut()
    else
        self:FlagTween()
    end
end
function UIBattleBGMap:FlagTween(reset)
    local current = dataTable.getDataTableByStringId("t_stage",MapManager.getMapId())
    local curItem = self.levelItemList[current.f_StageNum ]
    if not reset and HelperFunc.IsNull(self.FlagComp) then --创建新的，之前改curItem坐标有问题
        self.FlagComp = newGComponent("ChapterLevel","BattleUI")
        self.FlagComp:GetController("type").selectedIndex = curItem:GetController("type").selectedIndex
        self.FlagComp:GetChild("level").text = curItem:GetChild("level").text
        local Text_UnLockFunc = self.FlagComp:GetChild("Text_UnLockFunc");
        Text_UnLockFunc.text = curItem:GetChild("Text_UnLockFunc").text
        Text_UnLockFunc.onClick:Clear();
        Text_UnLockFunc.onClick:Add(function()
            --if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Task, false) then
            --    showUI(PanelNames.UITaskInfo, EnumConst.TaskType.unlockDesc);
            --end
            if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.UnlockFunc, false) then
                showUI(PanelNames.UIFuncUnlock, current.f_StageId);
            end
        end);

        self.FlagComp.displayObject.gameObject.name = "clone"
        curItem.parent:AddChild(self.FlagComp)
        self.FlagComp:SetXY(curItem.x,curItem.y)
    end
    ---@type FairyGUI.GLoader3D
    local eff = self.FlagComp:GetChild("effect_qizi")

    local nextItem = self.levelItemList[current.f_StageNum + 1]
    if not nextItem then return end
    if reset then
        curItem.visible = true
        local transition = nextItem:GetTransition("effect_fadein")
        transition:Play()
        if not HelperFunc.IsNull(self.FlagComp) then
            self.FlagComp:Dispose()
            self.FlagComp = nil
        end
        --[[Util.PlayAction(eff.wrapTarget,"idle1",true)
        local eff2 = nextItem:GetChild("effect_qizi")
        eff.x = eff2.x
        eff.y = eff2.y]]
    else
        curItem.visible = false
        local time = Util.PlayAction(eff.wrapTarget,"idle2",false)
        local x,y = ObjUtil.TransformPoint(nextItem,eff.xy,curItem)
        --烟雾out
        local transition = self.FlagComp:GetTransition("effect_fadeout")
        transition:Play(function()
            --去掉起跳和回弹时间
            eff:TweenMove(Vector2(x,y),time-0.7):SetEase(FairyGUI.EaseType.Linear):SetDelay(0.1) :OnComplete(function()
                self:DelayFinished()
            end)
        end)

    end

end

function UIBattleBGMap:DelayFinished()
    self:DoRefresh(true)
    self:FlagTween(true)
end
function UIBattleBGMap:DoRefresh(isLatest)
    if not self.isCreate then return end
    local id = MapManager.getMapId()
    if isLatest then
        local current = dataTable.getDataTableByStringId("t_stage",id)
        if current and current.f_NextStage > 0 then
            id = current.f_NextStage
        end
    end
    --if id == self.curMapId then
    --return
    -- end
    local oldId = self.curMapId
    self.curMapId = id
    local cfg = dataTable.getDataTableByStringId("t_stage",id)
    if not cfg then
        logError("t_stage 找不到该关卡:"..id)
        return
    end
    if self.ctrlChapter.pageCount < cfg.f_ChapterId + 1 then
        logError("UINewOfflineSence chapter 控制器找不到章节:"..cfg.f_ChapterId)
        return
    end
    self.curChapterId = cfg.f_ChapterId
    self.ctrlChapter.selectedIndex = cfg.f_ChapterId
    local levelCfList = self:GetLevelList(cfg.f_ChapterId)
    self.childMinIndex = 100000
    self.sortTempList = {}
    self.sortIndexList = {}
    for k,v in pairs(self.levelItemList) do
        local cfg = levelCfList[k]
        if cfg then
            v.visible = true
            table.insert(self.sortTempList,v)
            local index = v.parent:GetChildIndex(v)
            self.childMinIndex = math.min(index,self.childMinIndex)
            local ctrlType = v:GetController("type")
            local levelText = v:GetChild("level")
            local unlockText = v:GetChild("Text_UnLockFunc")
            local Btn_ChapterBox = v:GetChild("Btn_ChapterBox")
            local ctrlReward = v:GetController("box")
            levelText.text = cfg.f_ChapterId .. "-" .. cfg.f_StageNum;
            ctrlReward.selectedIndex = 0
            if cfg.f_StageId == id then
                ctrlType.selectedIndex = LevelType.arrive
                self.sortIndexList[v.name] = 20
            elseif cfg.f_StageId < id then
                ctrlType.selectedIndex = LevelType.pass
            --elseif cfg.f_type == 0 then -- boss关
            --    ctrlType.selectedIndex = LevelType.elite
            elseif cfg.f_type == 1 then
                ctrlType.selectedIndex = LevelType.boss
                self.sortIndexList[v.name] = 30
            elseif cfg.f_type == 3 then
                ctrlType.selectedIndex = LevelType.more
            else
                ctrlType.selectedIndex = LevelType.unPass
            end
            Btn_ChapterBox.onClick:Clear()
            if MapManager.IsEndChapterMapId(cfg.f_StageId) then
                self.ChapterBoxEff = v:GetChild("Effect_ChapterBox")
                Util.PlayAction(self.ChapterBoxEff.wrapTarget,"idle1",true);
                local hasChapterReward ,isChapterGet = logicMgr.MapManager.GetChapterReward(id)
                if hasChapterReward then
                    if isChapterGet then
                        ctrlReward.selectedIndex = 2
                    else
                        ctrlReward.selectedIndex = 1
                    end
                end
                Btn_ChapterBox.onClick:Add(self.ClickLastItem,self)
            end
            unlockText.onClick:Clear();
            if not string.IsNilOrEmpty( cfg.f_UnlockText) then
                if not self.sortIndexList[v.name] then
                    self.sortIndexList[v.name] = 10
                end
                unlockText.text = getLanguage(cfg.f_UnlockText)
                unlockText.onClick:Add(function()
                    --if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Task, false) then
                    --    showUI(PanelNames.UITaskInfo, EnumConst.TaskType.unlockDesc);
                    --end
                    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.UnlockFunc, false) then
                        showUI(PanelNames.UIFuncUnlock,cfg.f_StageId);
                    end
                end);
            else
                unlockText.text = ""
            end

            local btn_clear_tips = v:GetChild("Btn_ClearTips")
            btn_clear_tips.onClick:Clear()
            if cfg.f_StageId == id then
                btn_clear_tips.onClick:Add(function() 
                    logicMgr.UIShowManager.ShowRewardPreviewByJson2(cfg.f_StageReward, cfg.f_StageRewardHero, "通关当前关卡可获得")
                end)
            end
        else
            v.visible = false
        end
    end
    --[[if oldId ~= id then
        table.sort(self.sortTempList,function(a,b)
            local index_a = a:GetController("type").selectedIndex
            local index_b = b:GetController("type").selectedIndex
            if index_a ~= index_b then
                return index_a < index_b
            end
            return a.name < b.name
        end)
        for index, com in ipairs(self.sortTempList) do
            com.parent:SetChildIndex(com,self.childMinIndex + index - 1)
        end
    end]]
    if self.oldChapterId ~= self.curChapterId then
        self:LoadBGEffectByChapter(self.curChapterId)
    end

    self.oldChapterId = self.curChapterId
    self:RefreshRob()
end
--突袭刷新
function UIBattleBGMap:RefreshRob()
    --[[local isOpen = plunderHandler.IsOpen() and logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Rob)
    local cfg = dataTable.getDataTableByStringId("t_stage",self.curMapId)
    if not cfg then
        logError("t_stage 找不到该关卡:"..self.curMapId)
        return
    end
    local levelCfList = self:GetLevelList(cfg.f_ChapterId)
    local num = #levelCfList

    local show_num = dataTable.getGlobalInt( 521) or 1
    if num < show_num then
        show_num = num
    end

    self.randomRob = self:GetRobRandomLevel(levelCfList,show_num)
    local myPower = logicMgr.HeroManager.GetHerosSumPower()
    for i = 1,num do
        local item = self.levelItemList[i]
        local ctrlRob = item:GetController("Rob")
        item.onClick:Clear()
        if isOpen and self.randomRob[i] then
            ctrlRob.selectedIndex = 1
            local Head = item:GetChild("Head")
            local icon = Head:GetChild("head"):GetChild("HeadIcon")
            local frame = Head:GetChild("frame")
            local ctrlPower = Head:GetController("Power")
            local data = self.randomRob[i]
            ctrlPower.selectedIndex = data.total_power > myPower and 1 or 2
            UIComponentInterface:SetPlayerIcon(icon,data.pic,frame,data.picFrame)
            item.onClick:Add(function()
                plunderHandler.SetItemInfoFlagState( data.playerId, true )
                showUI(PanelNames.UIRob, data)
            end)
        else
            ctrlRob.selectedIndex = 0
        end
    end]]
    table.sort(self.sortTempList,function(a,b)
        local index_a = self.sortIndexList[a.name] or 0
        local index_b = self.sortIndexList[b.name] or 0
        if index_a ~= index_b then
            return index_a < index_b
        end
        --local ai = tonumber(a.name)
        --local bi = tonumber(b.name)
        --local aRob = self.randomRob[ai] == nil and 0 or 1
        --local bRob = self.randomRob[bi] == nil and 0 or 1
        --if aRob ~= bRob then
        --    return aRob < bRob
        --end
        return a.name > b.name
    end)
    for index, com in ipairs(self.sortTempList) do
        com.parent:SetChildIndex(com,self.childMinIndex + index - 1)
    end
end
function UIBattleBGMap:RefreshRobPower()
    if not self.randomRob then return end
    local myPower = logicMgr.HeroManager.GetHerosSumPower()
    for k,v in pairs(self.randomRob) do
        local item = self.levelItemList[k]
        local Head = item:GetChild("Head")
        local ctrlPower = Head:GetController("Power")
        ctrlPower.selectedIndex = v.total_power > myPower and 1 or 2
    end
end
function UIBattleBGMap:GetRobRandomLevel(levelCfgList,show_num)
    local showList = plunderHandler.GetCurPlunderSearchData()
    show_num = math.min(show_num,#showList)
    local randomList = TablePool.Get()
    local randomCount = 0
    local _list = {}
    local num = #levelCfgList
    local curIndex = dataTable.getDataTableByStringId("t_stage",self.curMapId,"f_StageNum")
    for k,v in pairs(levelCfgList) do--去除相邻两个
        if k ~= curIndex and k~= curIndex -1 and k ~= curIndex + 1 and k ~= num and k ~= num -1 then
            table.insert(randomList,k)
            randomCount = randomCount + 1
        end
    end
    show_num = math.min(show_num,#randomList)
    for i = 1,show_num do
        local r = math.random(1,#randomList)
        local value = randomList[r]
        table.ArrayRemoveByValue(randomList,value)
        table.ArrayRemoveByValue(randomList,value-1)--去除相邻两个
        table.ArrayRemoveByValue(randomList,value+1)
        _list[value] = showList[i]
        if #randomList == 0 then
            break
        end
    end
    TablePool.ReleaseDeeper(randomList,false)
    return _list
end
function UIBattleBGMap:FadeOut()
    if not self.isCreate then return end
    local transition = self:GetTransition("Effect_ChapterFadeOut")
    transition:Play(function()
        self:TransitionChange()
    end)
end
function UIBattleBGMap:TransitionChange()
    self:DoRefresh(true)
    local chapter = dataTable.getDataTableByStringId("t_chapter",self.curChapterId)
    local transition = self:GetTransition(chapter.f_ChapterAnimation)
    if chapter and transition ~= nil then
        transition:Play(function()
            self:FadeIn()
        end)
    else
        logError("找不到t_chapter动效："..self.curChapterId)
        self:FadeIn()
    end

end
function UIBattleBGMap:IsTransition()
    return self.isStartEff
end
function UIBattleBGMap:FadeIn()
    local transition = self:GetTransition("Effect_ChapterFadeIn")
    transition:Play()
    self.isStartEff = false
    FireEvent(Config.EventType.BattleBGMapFadeIn)
    self:UnloadBGEffect(logicMgr.MapManager.getMapId(),true)
end

function UIBattleBGMap:GetLevelList(chapterId)
    if (self.oldChapterId ==chapterId) then
        return self.levelCfgList
    end
    table.clear(self.levelCfgList)
    local cfgs = dataTable.getDataTable("t_stage")
    for k,v in pairs(cfgs) do
        if v.f_ChapterId == chapterId and v.f_StageId > 0 then
            table.insert(self.levelCfgList,v)
        end
    end
    table.sort(self.levelCfgList,function(a,b)
        return a.f_StageId < b.f_StageId
    end)
    return self.levelCfgList
end
function UIBattleBGMap:LoadBGEffect(dungeonId)
    local cfg = dataTable.getDataTableByStringId("t_stage",dungeonId)
    if not cfg then
        return
    end
    local chapterId = cfg.f_ChapterId
    self:LoadBGEffectByChapter(chapterId)
end
function UIBattleBGMap:LoadBGEffectByChapter(chapterId)
    local cfg = dataTable.getDataTableByStringId("t_chapter",chapterId)
    if not cfg then return end
    local eff = decodeJsonStrToTabel(cfg.f_ChapterEffect)
    for k,v in pairs(eff) do
        local graph = self.bgEffectGraphList[v]
        if graph then
            local data = self.bgEffectDataList[v]
            if not data or data.obj == nil then
                resMgr:LoadModelPrefab("Prefabs/UISpine/"..v,function(obj1)
                    if not self.isCreate then return end
                    if data and data.obj then return end
                    local o = newObject(obj1)
                    local tran = o.transform
                    if not data then
                        data = {obj = o,wrap = GoWrapper.New()}
                    else
                        data.obj = o
                    end
                    o:AddComponent(TCanvas)
                    data.wrap:SetWrapTarget(o,false)
                    graph:SetNativeObject(data.wrap)
                    tran.localPosition = Vector3(0,0,0)
                    self.bgEffectDataList[v] = data
                end)
            end
        end
    end
end
--onlyUnloadExtra 只卸载多余特效
function UIBattleBGMap:UnloadBGEffect(dungeonId,onlyUnloadExtra)
    local cfg = dataTable.getDataTableByStringId("t_stage",dungeonId)
    if not cfg then
        return
    end
    local chapterId = cfg.f_ChapterId
    self:UnloadBGEffectByChapter(chapterId,onlyUnloadExtra)
end
function UIBattleBGMap:UnloadBGEffectByChapter(chapterId,onlyUnloadExtra)
    local cfg = dataTable.getDataTableByStringId("t_chapter",chapterId)
    if not cfg then return end
    local excludeList
    if onlyUnloadExtra then
        local chapter = dataTable.getDataTableByStringId("t_chapter",self.curChapterId)
        if chapter then
            excludeList = {}
            local tb = decodeJsonStrToTabel(chapter.f_ChapterEffect)
            for k,v in pairs(tb) do
                excludeList[v] = true
            end
        end
    end
    local eff = decodeJsonStrToTabel(cfg.f_ChapterEffect)
    for k,v in pairs(eff) do
        local data = self.bgEffectDataList[v]
        local flag = not (excludeList and excludeList[v])
        if data and data.obj and flag then
            destroy(data.obj)
            data.obj = nil
        end
    end
end
function UIBattleBGMap:UnloadAllBGEffect()
    for k,v in pairs(self.bgEffectDataList) do
        if v.obj then
            destroy(v.obj)
            v.obj = nil
        end
        if v.wrap then
            v.wrap:Dispose()
        end
    end
    self.bgEffectGraphList = nil
    --table.clear(self.bgEffectGraphList)
end
function UIBattleBGMap:LoadSmoke()
    resMgr:LoadModelPrefab("UIEff/efc_map_yanwu",function(prefab)
        if not self.isCreate then
            return
        end
        if self.goWrapperSmoke.wrapTarget then
            destroy(self.goWrapperSmoke.wrapTarget)
        end
        local go = newObject(prefab)
        self.goWrapperSmoke:SetWrapTarget(go,false)
        self.Graph_YanWu:SetNativeObject(self.goWrapperSmoke)
    end)
end
function UIBattleBGMap:UpdateWeatherTime()
    if HelperFunc.GetServerTime() - self.weatherTime >= self.weatherChangeTime then
        self.weatherTime = HelperFunc.GetServerTime()
        self:LoadWeather()
    end
end
function UIBattleBGMap:LoadWeather()
    self.weatherIndex = (self.weatherIndex) % #weather + 1
    resMgr:LoadModelPrefab("Prefabs/MapPref/"..weather[self.weatherIndex],function(prefab)
        if not self.isCreate then
            return
        end
        if self.isDestroy then
            return
        end
        if self.goWrapper.wrapTarget then
            destroy(self.goWrapper.wrapTarget)
        end
        local go = newObject(prefab)
        self.goWrapper:SetWrapTarget(go,false)
        self.Graph_Weather:SetNativeObject(self.goWrapper)
    end)
end
function UIBattleBGMap:StartChapterEffect()
    if self.isStartEff then
        return
    end
    GRoot.inst.touchable = false
    self.isStartEff = true
    self:StartEffectDone()
    --local time = Util.PlayAction(self.ChapterBoxEff.wrapTarget,"idle2",false);
    --self.openBoxEffTimerId = self:TimeCall(self.StartEffectDone,time*1000)
end
function UIBattleBGMap:StartEffectDone()
    GRoot.inst.touchable = true
    logicMgr.UIShowManager.ShowUIChapterBox(function()
        local taskCfg = logicMgr.MapManager.GetChapterRewardCfg(self.rewardChapter)
        --local _ ,_,chapterTaskCfg = logicMgr.MapManager.GetChapterReward(MapManager.getMapId())
        log("请求领取任务t_task id:"..tostring(taskCfg.f_TaskId))
        taskInfoHandler.SendReqTaskRewards(taskCfg.f_TaskId)
        self.startEffTimerId =utimer.delayCall(function()
            self.isStartEff = false
        end,2000)
    end)
end
function UIBattleBGMap:ClickGetChapterReward()
    local hasPre,preChapter = logicMgr.MapManager.HasChapterRewardByPre()
    self.hasPreChapterReward = hasPre
    if hasPre then
        self.rewardChapter = preChapter
    else
        local hasChapterReward ,isChapterGet,chapterTaskCfg = logicMgr.MapManager.GetChapterReward(MapManager.getMapId())
        if not chapterTaskCfg then return end
        self.rewardChapter = logicMgr.MapManager.GetCurrentChapterId()
    end
    self:StartChapterEffect()
end
function UIBattleBGMap:ClickLastItem()
    local isEndLevel = logicMgr.MapManager.IsEndChapterMapId(MapManager.getMapId())
    local hasChapterReward ,isChapterGet,chapterTaskCfg = logicMgr.MapManager.GetChapterReward(MapManager.getMapId())
    if isEndLevel and hasChapterReward and isChapterGet then
        isEndLevel = false
        hasChapterReward ,isChapterGet,chapterTaskCfg = logicMgr.MapManager.GetChapterReward(self.curMapId)
    end
    if not isEndLevel then
        logicMgr.UIShowManager.ShowRewardPreviewByJson(chapterTaskCfg.f_Award,getLanguage("ChapterRewardText"))
        return
    end
    self:ClickGetChapterReward()
end
function UIBattleBGMap:OnEnable()
    if not self.isCreate then return end
    if panelMgr.BGFGUICameraMain then
        panelMgr.BGFGUICameraMain.gameObject:SetActive(true)
    end
    --self.fguiObj.visible = true
    self.isStartEff = false
    self:RefreshRobPower()
end
function UIBattleBGMap:OnDisable()
    if not self.isCreate then return end
    if panelMgr.BGFGUICameraMain then
        panelMgr.BGFGUICameraMain.gameObject:SetActive(false)
    end
    --if not HelperFunc.IsNull(self.fguiObj) then
    --    self.fguiObj.visible = false
    --end
end
function UIBattleBGMap:OnDestroy()
    UIComponentInterface:DelReferenceAll()
    CloseBGUIPanel()
    self.isDestroy = true
    if self.goWrapper.wrapTarget then
        destroy(self.goWrapper.wrapTarget)
    end
    if self.goWrapperSmoke.wrapTarget then
        destroy(self.goWrapper.wrapTarget)
    end
    if self.btnMap then
        self.btnMap.onClick:Clear()
    end
    self:UnloadAllBGEffect()
    self.goWrapperSmoke = nil
    self.goWrapper = nil
    self.isCreate = false
    self.levelItemList = nil
    self.fguiObj = nil
    self.curMapId = -1
    self.bgEffectGraphList = nil
    self.bgEffectDataList = nil
    self.randomRob = nil
    self.sortTempList = nil
    self.sortIndexList = nil
    --self.levelCfgList = nil
    CloseEvent(Config.EventType.ChapterTaskRewardGetDone,self.ChapterTaskRewardGetDoneMsg,self)
    CloseEvent(Config.EventType.Refresh_PlunderInfo,self.Refresh_PlunderInfo,self)
    CloseEvent(Config.EventType.Refresh_OpenPlunder,self.Refresh_PlunderInfo,self)
end
function UIBattleBGMap:ChapterTaskRewardGetDoneMsg()
    utimer.remove(self.startEffTimerId)
    self.startEffTimerId = nil
    if self.hasPreChapterReward then
        self.isStartEff = false
        self:Refresh(false)
        FireEvent(Config.EventType.BattleBGMapFadeIn)
    else
        self.isStartEff = true
        self:Refresh(true)
        FireEvent(Config.EventType.BattleBGMapFadeIn,true)
    end
    logicMgr.FlashGiftManager.showNewFlashGift();
    logicMgr.FlashGiftManager.showNewFlashGift();
end
function UIBattleBGMap:Refresh_PlunderInfo()
    self:RefreshRob()
end

return UIBattleBGMap